var express = require('express');
var router = express.Router();
var User = require('../db/user.js');
var article = require('../db/article');
var dynamic = require('../db/dynamic');


//获取未读评论数量
router.post('/getMessageNum', function (req, res, next) {
    // console.log(req.cookies);

    async function getNoRead() {
        let CommentMessageNum = 0;
        await article.find({ uid: req.cookies.userID }, function (err, AsCo) {
            if (err) { console.log(err); }
            for (let i = 0; i < AsCo.length; i++) {
                for (let j = 0; j < AsCo[i].comment.length; j++) {
                    if (AsCo[i].comment[j].c_type == false) {
                        CommentMessageNum++;
                    }
                }
            }
        });
        await dynamic.find({ uid: req.cookies.userID }, function (err, DyCo) {
            if (err) { console.log(err); }
            for (let i = 0; i < DyCo.length; i++) {
                for (let j = 0; j < DyCo[i].comment.length; j++) {
                    if (DyCo[i].comment[j].c_type == false) {
                        CommentMessageNum++;
                    }
                }
            }
        });
        return CommentMessageNum;
    };
    getNoRead().then(result => {
        // console.log(result);
        res.json({
            CoMsNum: result,
        });
    });

});

//退出登录
router.post('/dologout', function (req, res, next) {
    res.json({
        data: "已登出",
        uid: req.cookies.userID,
    });
});

router.post('/dologin1', function (req, res, next) {
    let username = req.body.username;
    let password = req.body.password;
    User.find({ account: username }, (err, data) => {
        if (err) {
            console.log(err);
            return;
        } else if (data == "") {
            res.send("该账号不存在！请先注册！");
            // console.log(data);
        } else {
            User.find({ pwd: password }, (err, data) => {
                if (err) {
                    console.log(err);
                    return;
                } else if (data == "") {
                    res.send("密码输入错误！");
                } else {
                    // console.log(data);
                    res.cookie("userID", data[0].uid, { httpOnly: true });
                    res.json({
                        data: "success",
                        uid: data[0].uid,
                        img_head: data[0].img_head,
                    });
                }
            })
        }
    })

})
router.post('/dologin2', function (req, res, next) {
    let username = req.body.username;
    User.find({ account: username }, (err, data) => {
        if (err) {
            console.log(err);
            return;
        } else if (data == "") {
            res.send("您的账号不存在，请先注册！");
        } else {
            // console.log(data);
            res.cookie("userID", data[0].uid, { httpOnly: true });
            res.json({
                data: "success",
                uid: data[0].uid,
                img_head: data[0].img_head,
            });
        }
    })
})
router.post('/doregister', function (req, res, next) {
    let username = req.body.username;
    let password = req.body.password;
    User.find({ account: username }, (err, data) => {
        if (err) {
            console.log(err);
            return;
        } else if (data == "") {
            let num = "";
            for (let i = 0; i < 6; i++) {
                num += Math.floor(Math.random() * 10);
            }
            let user = User({
                uid: username + num,
                account: username,
                pwd: password,
                uname: num,
            });
            user.save(function (err, datas) {
                if (err) {
                    res.send("注册失败，请重试！");
                    return;
                }
                res.cookie("userID", data[0].uid, { httpOnly: true });
                res.json({ data: "success", uid: data[0].uid, });
            });
        } else {
            res.send("您已注册过账号！请使用密码或短信验证登录！");
        }
    })
})

module.exports = router;